package cn.jingzhuan.element.demo.view

import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.os.Bundle
import android.support.v7.widget.Toolbar
import android.view.MenuItem
import cn.jingzhuan.element.demo.view.util.ThemeUtil
import cn.jingzhuan.element.element.R

abstract class BaseBindingActivity<T: ViewDataBinding> : BaseActivity(){

    lateinit var binding:T

    override fun onCreate(savedInstanceState: Bundle?) {
        ThemeUtil.configThemeBeforeOnCreate(this, R.style.AppTheme, R.style.LightTheme)
        super.onCreate(savedInstanceState)

        binding = createDataBinding(getLayoutId())
        onDataBind(savedInstanceState, binding)
    }

    protected abstract fun onDataBind(savedInstanceState: Bundle?, binding: T)

    private fun createDataBinding(layoutId: Int): T {
        return DataBindingUtil.setContentView(this, layoutId)
    }


    protected fun setUpActionBar(toolbar: Toolbar) {
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setHomeAsUpIndicator(R.drawable.ico_back)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item?.itemId == android.R.id.home) {
            onBackPressed()
        }
        return true
    }
}
